package cn.ym.org.service.impl;

import cn.ym.basic.service.impl.BaseServiceImpl;
import cn.ym.basic.utils.AssertUtil;
import cn.ym.basic.utils.Md5Utils;
import cn.ym.basic.utils.StrUtils;
import cn.ym.org.domain.Employee;
import cn.ym.org.mapper.EmployeeMapper;
import cn.ym.org.service.EmployeeService;
import cn.ym.user.domain.Logininfo;
import cn.ym.user.domain.User;
import cn.ym.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 业务层：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Transactional
    @Override
    public void add(Employee employee) {
        //1.处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        //2.创建Logininfo对象
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);//属性同名原则可以复制
        logininfo.setType(0);//0=管理员
        logininfo.setDisable(true);//启用
        //3.将Logininfo对象添加到数据库，并生成自增id
        logininfoMapper.add(logininfo);
        //4.将logininfo_id设置到employee对象中
        employee.setLogininfoId(logininfo.getId());
        //保存employee
        super.add(employee);

        //保存角色表的数据
        if (employee.getRoleId()!=null){
            //如果有新角色，添加新角色
            employeeMapper.addRole(employee);
        }

    }
    @Transactional
    @Override
    public void update(Employee employee) { //员工id，角色id
        //如果以前角色表有旧的角色，删除以前的旧角色
        employeeMapper.deleteRole(employee.getId());
        //如果有新角色，添加新角色
        if (employee.getRoleId()!=null){//前端选择了角色
            employeeMapper.addRole(employee);
        }
        //1.处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //2.查询到原来的employee对象
        Employee oldEmployee = employeeMapper.findOne(employee.getId());
        employee.setShopId(oldEmployee.getShopId());

        Logininfo oldlogininfo = logininfoMapper.findOne(oldEmployee.getLogininfoId());

        //3.更新数据t_logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);//属性同名原则可以复制
        logininfo.setId(oldlogininfo.getId());
        logininfo.setType(oldlogininfo.getType());
        logininfo.setDisable(oldlogininfo.getDisable());
        //更新logininfo
        logininfoMapper.update(oldlogininfo);
        //更新user
        super.update(employee);
    }
    @Transactional
    @Override
    public void delete(Long id) {
        //如果以前角色表有旧的角色，删除以前的旧角色
        employeeMapper.deleteRole(id);

        //1.查询user对象
        Employee employee = employeeMapper.findOne(id);
        //2.删除logininfo信息
        logininfoMapper.delete(employee.getLogininfoId());
        //3.删除employee
        super.delete(id);
    }
    @Transactional
    @Override
    public void patchDel(Long[] ids) {

        for (Long id : ids) {
            delete(id);
        }
    }
}
